@extends('Index/vip/vip_base')
@section('con-right')
	<div class="pull-right">
				<div class="user-content__box clearfix bgf">
					<div class="title">账户信息-编辑收货地址</div>
					<form action="/index/vip/udai_address_update" class="user-addr__form form-horizontal" method="post" role="form" id="form">
						<input type="hidden" name="id" value="{{$piece->id}}">
						<p class="fz18 cr">编辑收货地址<span class="c6" style="margin-left: 20px">电话号码、手机号码选填一项，其余均为必填项</span></p>
						<div class="form-group">
							<label for="name" class="col-sm-2 control-label">收货人姓名：</label>
							<div class="col-sm-6">
								<input class="form-control" id="name" name="name" value="{{$piece->cnee}}" placeholder="请输入姓名" type="text">
							</div>
						</div>
						<div class="form-group">
							<label for="details" class="col-sm-2 control-label">收货地址：</label>
							<div class="col-sm-10">
								<div class="addr-linkage">
									<select name="province"></select>
									<select name="city"></select>
									<select name="area"></select>
									<select name="town"></select>
								</div>
								<input class="form-control" id="details" name="details" placeholder="建议您如实填写详细收货地址，例如街道名称，门牌号码等信息" maxlength="30" value="{{$piece->details}}" type="text">
							</div>
						</div>
						<div class="form-group">
							<label for="mobile" class="col-sm-2 control-label">手机号码：</label>
							<div class="col-sm-6">
								<input class="form-control" id="mobile" name="tel" value="{{$piece->tel}}" placeholder="请输入手机号码" type="text">
							</div>
						</div>
						<!-- <div class="form-group">
							<div class="col-sm-offset-2 col-sm-6">
								<div class="checkbox">
									<label><input type="checkbox" checked><i></i> 设为默认收货地址</label>
								</div>
							</div>
						</div> -->
						<div class="form-group">
							<div class="col-sm-offset-2 col-sm-6">
								<button id="edit" type="submit" class="but">保存</button>
							</div>
						</div>
						<script src="/index/js/jquery.citys.js"></script>
						
						<script>
							$(document).ready(function(){
								// 添加街道/乡镇
								function townFormat(info){
									$('.addr-linkage select[name="town"]').hide().empty();
									if (info['code'] % 1e4 && info['code'] < 7e6){	//是否为“区”且不是港澳台地区
										var ajaxConfig = {
											url: 'http://passer-by.com/data_location/town/' + info['code'] + '.json',
											scriptCharset:'UTF-8',
											dataType: "json",
											timeout: 4000,
											success: function(data) {
												$('.addr-linkage select[name="town"]').show();
												// $('#code').val(info['code']) // 填地区编码
												for (i in data) {
													$('.addr-linkage select[name="town"]').append(
														'<option value="' + data[i] + '">' + data[i] + '</option>'
													);
												};
												$('.addr-linkage select[name="town"]').find('option[value="{{$address[3]}}"]').prop("selected", "selected");
											},
										};
										$.ajax(ajaxConfig).fail(function(p1,p2,p3){
											ajaxConfig.url = 'js/data_location/town/' + info['code'] + '.json';
											$.ajax(ajaxConfig)
										});
									}
								};
								$('.addr-linkage').citys({
									// 如果某天这个仓库地址失效了dataUrl请使用本地 2017.10 的数据 'js/data_location/list.json'
									dataUrl: 'http://passer-by.com/data_location/list.json',
									spareUrl: 'js/data_location/list.json',
									dataType: 'json',
									valueType: 'name',
									province: '{{$address[0]}}',
									city:'{{$address[1]}}',
									area: '{{$address[2]}}',
									onChange: function(data) {
										townFormat(data)
									},
								},function(api){
									var info = api.getInfo();
									townFormat(info);
								});
							}); 
						</script>

						<script>
							$('#edit').click(function(){
					            $.ajax({
					                url:'/index/vip/udai_address_update',
					                type:'POST',
					                cache:false,
					                data:$('#form').serialize(), //将表单信息序列化(无法处理文件流)
					                success:function(data){
					                    // window.parent.location.reload();
					                    if(data==1){
					                        layer.msg('修改成功',{'icon':1});
					                    }else{
					                        layer.msg('修改失败',{'icon':2});
					                    }
					                }
					            }).done(function(){
					                window.location.href="/index/vip/udai_address";
					            }).fail(function(){
					                layer.msg('请正确填写信息',{'icon':2});
					            })
					        })
						</script>
					</form>
				</div>
	</div>
@endsection